package cn.iune.web.erp.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;

import cn.iune.backcenter.author.sysinfo.ISysInfoService;
import cn.iune.backcenter.author.sysinfo.SysInfo;
import cn.iune.common.response.MapMessage;
import cn.iune.config.SecurityService;
import cn.iune.erp.inventory.IStorageRecordService;
import cn.iune.erp.inventory.StorageRecord;
import cn.iune.erp.invoice.AccountInvoice;
import cn.iune.erp.invoice.IInvoiceService;
import cn.iune.erp.purchase.IPurchaseOrderService;
import cn.iune.erp.purchase.IPurchaseService;
import cn.iune.erp.purchase.PurchaseOrder;
import cn.iune.erp.setting.IBillSettingOrgService;
import cn.iune.erp.setting.ISettingService;

@Controller
@RequestMapping(value="/purchase")
public class PurchaseController {

//	private final Logger log = LoggerFactory.getLogger(PurchaseController.class);
	
	
	@Autowired
	private IPurchaseOrderService purchaseOrderService;
	@Autowired
	private IPurchaseService stockService;
	
	@Autowired
	private IStorageRecordService storageRecordService;
	
	@Autowired
	private IInvoiceService invoiceService;
	
	@Autowired
	private ISysInfoService sysInfoService;
	
	
	@Autowired
	private IBillSettingOrgService billSettingOrgService;
	@Autowired
	private ISettingService settingService;
	@Autowired
	private IPurchaseService purchaseService;
	
	
	@RequestMapping(value="/purchaseOrder/single",method=RequestMethod.GET)
	public ModelAndView purchaseOrderSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/purchaseOrder/single");
		mav.addObject("path", "/purchase/purchaseOrder/single");
		return mav;
	}

	//////////////////////////对应前台BizData的接口////////////////////////////////

	@RequestMapping(value={
			"/purchaseOrder/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		JSONObject json = purchaseOrderService.newData(data);
		return json;
	}

	@RequestMapping(value="/purchaseOrder/single/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderDelete(@RequestBody JSONObject data) {
		
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");

		data.put("ucode", ucode);
		data.put("uname", uname);
		data.put("uid", uid);
		
		return purchaseOrderService.deleteData(data);
	}

	@RequestMapping(value="/purchaseOrder/single/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderSave(@RequestBody JSONObject data) {
		
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");

		data.put("ucode", ucode);
		data.put("uname", uname);
		data.put("uid", uid);

		return purchaseOrderService.saveData(data);
	}

	@RequestMapping(value={
			"/purchaseOrder/single/query"
//			,"/purchaseOrder/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderQuery(@RequestBody JSONObject data) {
		
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");

		data.put("ucode", ucode);
		data.put("uname", uname);
		data.put("uid", uid);
		
		data = SecurityService.addDataAccessPerm(data);
		return purchaseOrderService.queryData(data);
	}
	
	//////////////////////////对应前台BizData的接口////////////////////////////////


	@RequestMapping(value="/purchaseOrder/single/createDetail",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderCreateDetail(@RequestBody JSONObject data) {
		JSONObject json = purchaseOrderService.newDataDetail(data);
		
		return json;
	}

	@RequestMapping(value="/purchaseOrder/single/queryDetail",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderQueryDetail(@RequestBody JSONObject data) {
		
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");

		data.put("ucode", ucode);
		data.put("uname", uname);
		data.put("uid", uid);

		return purchaseOrderService.queryDataDetail(data);
	}

	
	
	

	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value="/purchaseOrder/single/confirm",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderConfirm(id, ucode, uname, uid);
	}

	@RequestMapping(value="/purchaseOrder/single/active",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderActive(id, ucode, uname, uid);
	}
	
	@RequestMapping(value="/purchaseOrder/single/agreeIt",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderAgreeIt(id, ucode, uname, uid);
	}
	
	@RequestMapping(value="/purchaseOrder/single/unAgree",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderUnAgree(id, ucode, uname, uid);
	}
	
	@RequestMapping(value="/purchaseOrder/single/close",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderClose(id, ucode, uname, uid);
	}

	@RequestMapping(value="/purchaseOrder/single/open",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> purchaseOrderOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String ucode = SecurityService.getCurrentPerson().getString("cUsercode");
		String uname = SecurityService.getCurrentPerson().getString("cUsername");
		String uid = SecurityService.getCurrentPerson().getString("userId");
		return stockService.purchaseOrderOpen(id, ucode, uname, uid);
	}

	@RequestMapping(value="/purchaseOrder/single/printPDF",method=RequestMethod.GET)
//	@ResponseBody
	public void purchaseOrderPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletResponse response) throws Exception {
		//stockService.purchaseOrderPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    stockService.purchaseOrderPrintPDF(id, doc);
		//关闭doc
	    doc.close();
	}
	
	//由采购订单生成采购进货单
	@RequestMapping(value = "/purchaseOrder/single/pushCGRK", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject pushCGRK(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("id");
		
		PurchaseOrder purchaseOrder = purchaseOrderService.getByID(id);
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		String autoState = "N";
		int flowType = 1;
		String autoPayment = "N";//现结
	
		//填写销售发货单
		StorageRecord obj = purchaseService.storageRecordCGRKInsert(purchaseOrder, cUsercode, cUsername, userId, autoState, flowType, autoPayment, employeeNo, employeeName);

//		StorageRecord obj = storageService.storageRecordCGRKInsert(purchaseOrder, billType, billTypeName, systemCode, flowType, revOpay, direction, costingCode, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, autoState, autoPayment, employeeNo, employeeName);
		

		/*
		JSONObject json = new JSONObject();
		if(obj==null){
			json.put("code", -1);
			json.put("status", "生成单据失败");
			return json;
		}else{
			json.put("code", 0);
			json.put("status", "成功");
			json.put("billNo", obj.getRecordNo());
			
		}
		
		return json;
		*/
	
		MapMessage mapMessage = null;
		
		if(obj==null){
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			return mapMessage.toJSONObject();
		}
		
		mapMessage = new MapMessage(obj.getfID(),obj.getRecordNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}
		
		return mapMessage.toJSONObject();
	
	}
	

	//////////////////////////////////////////////////////////////////////////////////////
	
//	@RequestMapping(value="/purchaseOrder/selectMulti",method=RequestMethod.GET)
//	public ModelAndView purchaseOrderSelectMulti(){
//		ModelAndView mav = new ModelAndView("/appcenter/purchase/purchaseOrder/selectMulti");
//		return mav;
//	}
	
	@RequestMapping(value="/purchaseOrder/selectSingle",method=RequestMethod.GET)
	public ModelAndView purchaseOrderSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/purchaseOrder/selectSingle");
		return mav;
	}

	@RequestMapping(value="/orderDetailQuery/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject purchaseOrderDetailQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return purchaseOrderService.queryDataDetail(data);
	}

	//选择采购订单明细表（用于进货单）
	@RequestMapping(value="/orderDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView purchaseOrderDetailQueryList(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/orderDetailQuery/list");
		return mav;
	}
	//选择采购订单明细表（用于进货单）
	@RequestMapping(value="/orderDetailSelect/selectMulti",method=RequestMethod.GET)
	public ModelAndView purchaseOrderDetailSelectMulti(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/orderDetailSelect/selectMulti");
		return mav;
	}

	//待审核单据数量
	@RequestMapping(value="/purchaseOrder/getUnAgreeCount",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getUnAgreeCount() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			long count = purchaseOrderService.getUnAgreeCount(enterpriseNo);
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
		
	}

	
	
	
	//采购进货
	@RequestMapping(value="/invCGRK/single",method=RequestMethod.GET)
	public ModelAndView invCGRKSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/invCGRK/single");
//		mav.addObject("path", mav.getViewName());
//		System.out.println(mav.getViewName());
		mav.addObject("path", "/purchase/invCGRK/single");
		return mav;
	}
	
	//采购退货
	@RequestMapping(value="/invCGCK/single",method=RequestMethod.GET)
	public ModelAndView invCGCKSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/invCGCK/single");
		mav.addObject("path", "/purchase/invCGCK/single");
		return mav;
	}

	@RequestMapping(value={
			"/invCGRK/single/query"
			,"/invCGCK/single/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return storageRecordService.queryData(data);
	}

	@RequestMapping(value={
			"/invCGRK/single/save"
			,"/invCGCK/single/save"
	},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKSave(@RequestBody JSONObject object) {
		return storageRecordService.saveData(object);
	}


	@RequestMapping(value={
			"/invCGRK/single/delete"
			,"/invCGCK/single/delete"
	},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKDelete(@RequestBody JSONObject data) {
		return storageRecordService.deleteData(data);
	}

	@RequestMapping(value={
			"/invCGRK/single/create"
			,"/invCGCK/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		JSONObject json = storageRecordService.newData(data);
		return json;
	}

	@RequestMapping(value={
			"/invCGRK/single/createDetail"
			,"/invCGCK/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKCreateDetail(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject json = storageRecordService.newDataDetail(data);
		return json;
	}


	@RequestMapping(value={
			"/invCGRK/single/queryDetail"
			,"/invCGCK/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invCGRKQueryDetail(@RequestBody JSONObject data) {
		return storageRecordService.queryDataDetail(data);
	}

	

	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value={
			"/invCGRK/single/confirm"
			,"/invCGCK/single/confirm"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return purchaseService.storageRecordConfirm(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/invCGRK/single/active"
			,"/invCGCK/single/active"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();

		return purchaseService.storageRecordActive(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}
	@RequestMapping(value={
			"/invCGRK/single/agreeIt"
			,"/invCGCK/single/agreeIt"
	},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return purchaseService.storageRecordAgreeIt(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/invCGRK/single/unAgree"
			,"/invCGCK/single/unAgree"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();

		return purchaseService.storageRecordUnAgree(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}
	@RequestMapping(value={
			"/invCGRK/single/close"
			,"/invCGCK/single/close"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		return purchaseService.storageRecordClose(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	@RequestMapping(value={
			"/invCGRK/single/open"
			,"/invCGCK/single/open"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invCGRKOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		return purchaseService.storageRecordOpen(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	//采购进货单打印
	@RequestMapping(value={
			"/invCGRK/single/printPDF"
			},method=RequestMethod.GET)
	public void invCGRKPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletResponse response) throws Exception {
		//storageService.invCGRKPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    purchaseService.invCGRKPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}

	//采购退单打印
	@RequestMapping(value={
			"/invCGCK/single/printPDF"
			},method=RequestMethod.GET)
	public void invCGCKPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletResponse response) throws Exception {
		//storageService.invXSCKPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    purchaseService.invCGCKPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}
	
	//退货单
	@RequestMapping(value={"/invCGRK/single/pushCGCK"},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> pushCGCK(@RequestBody JSONObject params, HttpServletRequest request) {
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");

		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		//return storageService.pushBACK(params, cUsercode, cUsername, userId, employeeNo, employeeName);
		
		String id = params.getString("id");
		
		//填写销售发货单
		StorageRecord storageRecord = storageRecordService.getByID(id);
		
		String autoState = "N";
		int flowType = 1;
		String autoPayment = "N";//现结
		
		//填写销售发货单
		StorageRecord obj = purchaseService.storageRecordBACKInsert(storageRecord, cUsercode, cUsername, userId, autoState, flowType, autoPayment, employeeNo, employeeName);
//		StorageRecord obj = storageService.storageRecordBACKInsert(storageRecord, billType, billTypeName, systemCode, flowType, revOpay, direction, costingCode, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, autoState, autoPayment, employeeNo, employeeName);

		MapMessage mapMessage = null;
		
		if(obj==null){
//			JSONObject json = new JSONObject();
//			json.put("code", -1);
//			json.put("status", "生成单据失败");
//			return json;
			
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			return mapMessage.toJSONObject();
		}
		
//		JSONObject json = new JSONObject();
//		json.put("code", 0);
//		json.put("status", "成功");
//		json.put("id", accountPayment.getfID());
//		json.put("billNo", accountPayment.getAccountPaymentNo());
//		return json;
		
		mapMessage = new MapMessage(obj.getfID(),obj.getRecordNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}

		return mapMessage.toJSONObject();
		
//		return storageService.pushBACK(params, billType, billTypeName, systemCode, flowType, revOpay, direction, costingCode, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, autoState, autoPayment, employeeNo, employeeName);
	}
	
	//由采购进货单生成采购发票
	@RequestMapping(value = {"/invCGRK/single/pushCGFP","/invCGCK/single/pushCGFP"}, method = RequestMethod.POST)
	@ResponseBody
	public JSONObject pushCGFP(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("rowID");
		StorageRecord storageRecord = storageRecordService.getByID(id);
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		int flowType = 1;
		int cashSettle = 0;//现结
		
		//填写付款单付款
		AccountInvoice obj = invoiceService.accountInvoiceInsert(storageRecord, flowType, cashSettle, cUsercode, cUsername, userId, employeeNo, employeeName);
		/*
		String billType = null;
		if("XSCK".equals(storageRecord.getBillType()) || "XSRK".equals(storageRecord.getBillType())){
			billType = "XSFP";//销售发票
		}
		if("CGRK".equals(storageRecord.getBillType()) || "CGCK".equals(storageRecord.getBillType())){
			billType = "CGFP";//采购发票
		}
		if("WWRK".equals(storageRecord.getBillType()) || "WWCK".equals(storageRecord.getBillType())){
			billType = "CGFP";//采购发票
		}
		String enterpriseNo = storageRecord.getEnterpriseNo();
		
		Map<String,Object> map = WebSystemService.getBillParam(billType, enterpriseNo, request);
		String billTypeName = (String) map.get("billTypeName");
		String systemCode = (String) map.get("systemCode");
		int flowType = (int) map.get("flowType");
		String revOpay = (String) map.get("revOpay");
		int direction = (int) map.get("direction");
//		String costingCode = (String) map.get("costingCode");
		String autoCode = (String) map.get("autoCode");
		String origin = (String) map.get("origin");
		String originName = (String) map.get("originName");
		String billNo = (String) map.get("billNo");
		
		String autoState = "N";
		flowType = 1;//自动审核
		String autoPayment = "N";//现结
		
		AccountInvoice obj = accountInvoiceService.accountInvoiceInsert(storageRecord, billType, billTypeName, systemCode, flowType, revOpay, direction, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, autoState, autoPayment, employeeNo, employeeName);
		*/

		/*
		JSONObject json = new JSONObject();
		if(obj!=null){
			json.put("code", 0);
			json.put("status", "成功");
			json.put("zdh", obj.getAccountInvoiceNo());
		}else{
			json.put("code", -1);
			json.put("status", "失败");
			
		}
		
		return json;
		*/

		MapMessage mapMessage = null;
		JSONObject json = new JSONObject();
		
		if(obj==null){
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			json =  mapMessage.toJSONObject();
			json.put("status", "失败");
			
			return json;
		}
		
		mapMessage = new MapMessage(obj.getfID(),obj.getAccountInvoiceNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}
		
		json = mapMessage.toJSONObject();
		json.put("status", "成功");
		json.put("zdh", obj.getAccountInvoiceNo());
		
		return json;

		
	}
	
	
	//由采购进货单生成会计凭证
	@RequestMapping(value = {"/invCGRK/single/pushKJPZ","/invCGCK/single/pushKJPZ"}, method = RequestMethod.POST)
	@ResponseBody
	public JSONObject pushKJPZ(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("rowID");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		//自动分录
		return purchaseService.accountingEntry(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	//////////////////////////////////////////////////////////////////////////////////////
	
	//采购进货明细表
	@RequestMapping(value="/invCGRKDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView invCGRKDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/invCGRKDetailQuery/list");
		return mav;
	}
	
	//采购退货明细表
	@RequestMapping(value="/invCGCKDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView invCGCKDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/invCGCKDetailQuery/list");
		return mav;
	}

	@RequestMapping(value= {
			"/invCGRKDetailQuery/list/query"
			,"/invCGCKDetailQuery/list/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject recordDetailQuery(@RequestBody JSONObject data,HttpServletRequest request) {
		data = SecurityService.addDataAccessPerm(data);
		return storageRecordService.queryDataDetail(data);
	}
	
	//选择采购进货单
	@RequestMapping(value="/invCGRK/selectSingle",method=RequestMethod.GET)
	public ModelAndView invCGRKSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/invCGRK/selectSingle");
		return mav;
	}

	
	
	
	
	//////////////////////////////////////////////////////////////////
	//单据属性设置--采购订单
	@RequestMapping(value="/billSettingOrg/cgdd",method=RequestMethod.GET)
	public ModelAndView billSettingOrgCGDD(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/billSettingOrg/cgdd");
		mav.addObject("path","/purchase/billSettingOrg/cgdd");
		return mav;
	}
	
	//单据属性设置--采购进货单
	@RequestMapping(value="/billSettingOrg/cgrk",method=RequestMethod.GET)
	public ModelAndView billSettingOrgCGRK(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/billSettingOrg/cgrk");
		mav.addObject("path","/purchase/billSettingOrg/cgrk");
		return mav;
	}
	
	//单据属性设置--采购退货单
	@RequestMapping(value="/billSettingOrg/cgck",method=RequestMethod.GET)
	public ModelAndView billSettingOrgCGCK(){
		ModelAndView mav = new ModelAndView("/appcenter/purchase/billSettingOrg/cgck");
		mav.addObject("path","/purchase/billSettingOrg/cgck");
		return mav;
	}
	
	@RequestMapping(value={
			"/billSettingOrg/cgdd/query",
			"/billSettingOrg/cgrk/query",
			"/billSettingOrg/cgck/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingOrgQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return billSettingOrgService.queryData(data);
	}

	@RequestMapping(value={
			"/billSettingOrg/cgdd/save",
			"/billSettingOrg/cgrk/save",
			"/billSettingOrg/cgck/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingOrgSave(@RequestBody JSONObject object) {
		return billSettingOrgService.saveData(object);
	}

	@RequestMapping(value={
			"/billSettingOrg/cgdd/valid",
			"/billSettingOrg/cgrk/valid",
			"/billSettingOrg/cgck/valid"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingOrgValid(@RequestBody JSONObject object) {
		return billSettingOrgService.validData(object);
	}
	
	//复制数据。从BillSetting到BillSettingOrg
	@RequestMapping(value="/billSettingOrg/loadData",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject billSettingOrgLoad(@RequestBody JSONObject data) {
		String billType = data.getString("billType");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		String enterpriseName = SecurityService.getCurrentOgn().getsLongName();
		return settingService.copyBillSetting(billType, userId, enterpriseNo, enterpriseName);
	}


}
